package com.lujieni.dubboconsumer.service;

import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.lujieni.dubbointerface.HelloService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

@Service
public class HelloHandler {

    @DubboReference(retries = 1, mock = "com.lujieni.dubbointerface.HelloServiceMock")
    private HelloService helloService;

    public String getNo(){
        try {
            return helloService.getNo();
        } catch (RuntimeException ex) {
            if (ex.getMessage().contains("SentinelBlockException: FlowException")) {
                return getNoLimit();
            }else if (ex.getMessage().contains("SentinelBlockException: DegradeException")) {
                return getNoFallback();
            }
        }
        return null;
    }

    private String getNoFallback(){
        return "我被熔断了";
    }

    private String getNoLimit(){
        return "我被限流了";
    }


}
